import { defHttp } from '/@/common/util/axios';

enum Api {
  list = '/base/baseSuitunitOrg/findPage', //用户列表查询
  // deptList = '/epm/km/dept/findAllTree', //部门树
  // asynDeptList = '/epm/km/dept/findTree', //异步部门树
  // create = '/base/baseSuitunitOrg/create', //新增绩效部门
  create = '/base/baseRel/updateOrgAndRel', //新增绩效部门
  update = '/base/baseSuitunitOrg/update', //修改绩效部门
  orgType = '/base/orgType/findPage', //组织部门类型
  orgList = '/base/stType/getSuitOrgList', //
  respUser = '/base/km/user/findAllTree', //查询部门负责人树
  // asynUser = '/base/km/user/findTree', //异步查询部门负责人树
  batchUpdate = '/base/baseSuitunitOrg/batchUpdate', //批量修改绩效部门
  getUnitTree = '/base/baseSuitunitOrg/findTree', //上级部门和绩效单位树

  // deptList = '/base/hubKm/findDeptAllTree', //部门树
  deptList = '/base/km/dept/findAllTree',
  asynDeptList = '/base/hubKm/findDeptTree', //异步部门树
  asynUser = '/base/hubKm/findUserTree', //异步人员树
  anaysisList = '/analysis/epmAnalysisBaseOrgType/list', // 分析类型下拉数据
  selectPersonTreeList = '/base/hubKm/getDeptAndPostTree',
}

/**
 * author:lhw
 * description: 分页查询
 * @params: @params 查询参数
 * date: 2023/2/13
 * lastModify: 2023/2/13
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * author:lhw
 * description: 查询部门树
 * @params: @
 * date: 2023/2/13
 * lastModify: 2023/2/13
 */

// export const getDeptTree = () => defHttp.get({ url: Api.deptList });
export const getDeptTree = () => defHttp.get({ url: `/system/dept/getListById?parentId=${5010000}` });

export const getAsynDeptTree = (params) => defHttp.get({ url: Api.asynDeptList, params }, { joinParamsToUrl: true });

export const getUnitTree = (params) => defHttp.get({ url: Api.getUnitTree, params });

/**
 * author:lhw
 * description: 新增绩效单位
 * @params: @
 * date: 2023/2/14
 * lastModify: 2023/2/14
 */
export const createUnit = (params) => {
  return defHttp.post({ url: Api.create, params });
};

/**
 * author:lhw
 * description: 单条修改绩效部门
 * @params: @
 * date: 2023/2/14
 * lastModify: 2023/2/14
 */
export const updateUnit = (params) => {
  return defHttp.post({ url: Api.update, params });
};

/**
 * author:lhw
 * description: 查询组织部门类型
 * @params: @
 * date: 2023/2/14
 * lastModify: 2023/2/14
 */
export const getOrgType = () => {
  const params = {
    pageNo: 1,
    pageSize: 100,
  };
  return defHttp.get({ url: Api.orgType, params });
};

function formatRes(list) {
  list.forEach((l) => {
    if (!l.isUser) {
      l.id = 'fa' + l.id;
      l.selectable = false;
    }
    if (l.children) {
      formatRes(l.children);
    }
  });
}

/**
 * author:lhw
 * description: 异步查询绩效负责人树
 * @params: @
 * date: 2023/2/14
 * lastModify: 2023/2/14
 */
export const getKeyUser = (params) => {
  return new Promise((resolve) => {
    defHttp.get({ url: Api.respUser, params }).then((res) => {
      formatRes(res);
      resolve(res);
    });
  });
};

export const getAsynKeyUser = (params) => {
  return new Promise((resolve) => {
    defHttp.get({ url: Api.asynUser, params }, { joinParamsToUrl: true }).then((res) => {
      res.forEach((r) => {
        if (!r.isUser) {
          r.selectable = false;
        }
      });
      resolve(res);
    });
  });
};

/**
 * author:lhw
 * description: 批量修改绩效部门
 * @params: @
 * date: 2023/2/14
 * lastModify: 2023/2/14
 */
export const batchUpdate = (params) => defHttp.post({ url: Api.batchUpdate, params });

export const anaysisList = () => defHttp.get({ url: Api.anaysisList });

export const selectPersonTreeList = (params?) => {
  const titleMap = (arr = []) => {
    arr.forEach((i) => {
      // i.title = i.orgName;
      i.value = i.id + '';
      // i.id = i.deptId;
      i.key = i.id + '';
      i.disabled = !i.isLeaf;
      i.psnNum = i.desc ? i.desc.psnNum : i.id + '';
      if (i.desc?.psnNum) {
        i.psnNum = i.desc.psnNum;
        // i.key = i.desc.psnNum + '';
      }
      if (i.children?.length) {
        titleMap(i.children);
      }
    });
  };
  return new Promise((resolve, reject) => {
    defHttp.get({ url: Api.selectPersonTreeList, params }).then((res) => {
      titleMap(res);
      console.log('res-------------岗位人员树', res);
      resolve(res);
    });
  });
  // return defHttp.get({ url: Api.selectSuperDepartTreeList, params });
};
